﻿/*----------------------------------------------------------------
// Copyright (C) 2016 通通优品版权所有。
// 命名空间: TongTongMall.DapperFramework.Repositories.User
// 类名：UserBaseInfoRepository
// 功能描述：N/A
// 
// 创建标识：Roc.Lee(李鹏鹏) 2017/9/1 17:08:16 TEC-ROCLEE Roc.Lee

// 修改标识：
// 修改描述：
// 
//
//----------------------------------------------------------------*/
using STOWaybillMall.Core;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Dapper;
using TongTongMall.Dapper.DapperExtensions;
using TongTongMall.MallWeb.Core.IRepositories;
using TongTongMall.MallWeb.Core.User.Models;

namespace TongTongMall.DapperFramework.Repositories.User
{
    public class UserBaseInfoRepository : TongTongMallDbContextBase<UserBaseInfo, long>, IUserBaseInfoRepository
    {
        public UserBaseInfoRepository(IDbContextProvider<IDatabase> dbContextProvider) : base(dbContextProvider)
        {
        }

        public long GetUserID(string account)
        {
            IList<IPredicate> predicate = new List<IPredicate>();
            predicate.Add(Predicates.Field<UserBaseInfo>(x => x.Email, Operator.Eq, account));
            predicate.Add(Predicates.Field<UserBaseInfo>(x => x.MobileNo, Operator.Eq, account));
            predicate.Add(Predicates.Field<UserBaseInfo>(x => x.UserName, Operator.Eq, account));
            //predicate.Add(Predicates.Field<UserBaseInfo>(x => x.Account, Operator.Eq, account));

            var group = new PredicateGroup
            {
                Operator = GroupOperator.Or,
                Predicates = predicate
            };
            //var groupTenant = new PredicateGroup
            //{
            //    Operator = GroupOperator.And,
            //    Predicates = new List<IPredicate> {
            //        group,
            //        Predicates.Field<UserBaseInfo>(x => x.TenantId, Operator.Eq, tenantId)
            //    }
            //};
            var user = base.FirstOrDefault(group, selector: x => new { x.UserID });
            if (user == null) return 0;
            return user.UserID;
        }

        public UserPreLoginCheckInfoDto GetUserPreLoginCheckInfo(string account)
        {

            return DB.Connection.QueryFirst<UserPreLoginCheckInfoDto>(
                @"SELECT [UserID]
      ,[UserName]
      ,[MobileNo]
      ,[Email]
      ,[UserStatus]
      ,[LoginPwd]
	   ,[PwdKey] 
	   ,[TicketVersion]
  FROM [UserBaseInfo] where UserName=@account OR MobileNo=@account OR Email=@account", new { account = account });
        }
    }
}
